#include "localdatabase.h"

LocalDataBase::LocalDataBase(QObject *parent): QObject(parent){
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("experiencelife.db"); // 数据库文件名为example.db
    if (!db.open()) {
        qDebug() << "无法打开数据库:" << db.lastError();
    }
}

bool LocalDataBase::writeStickyNote(int snOwner,QString snContent,long long timeWrite,long long timeWarn){  //写入便签纸
    QSqlQuery query;
    query.prepare("INSERT INTO stickynote (snowner,sncontent,timewrite,timewarn) VALUES (:snowner,:sncontent,:timewrite,:timewarn)");
    query.bindValue(":snowner", snOwner);
    query.bindValue(":sncontent", snContent);
    query.bindValue(":timewrite", QVariant::fromValue<qlonglong>(timeWrite));
    query.bindValue(":timewarn", QVariant::fromValue<qlonglong>(timeWarn));
    if (!query.exec()) {
        qDebug() << "插入失败:" << query.lastError();
        return false;
    }
    return true;
}

QVariantList LocalDataBase::readDataSN(int userId){//读取用户便签数
    QVariantList result;
    QSqlQuery query;
    query.prepare("SELECT * FROM stickynote WHERE snowner = ?");
    // 绑定实际值到第一个占位符
    query.addBindValue(userId);
    // 执行查询
    if (!query.exec()) {
        qDebug() << "Error executing query:" << query.lastError();
    }
    while (query.next()) {
        QVariantMap row;
        for (int i = 0; i < query.record().count(); ++i) {
            row[query.record().fieldName(i)] = query.value(i);
        }
        result.append(row);
    }
    return result;
}

bool LocalDataBase::delStickyNote(int userId,int snIndex){//删除用户便签
    QSqlQuery query;
    query.prepare("DELETE FROM stickynote WHERE id = :id AND snowner = :userId");
    query.bindValue(":id", snIndex);
    query.bindValue(":userId", userId);    // 执行查询
    if (!query.exec()) {
        qDebug() << "Error executing query:" << query.lastError();
    }
    return true;
}

QVariantList LocalDataBase::readWarnTimeSN(int userId){//读取用户便签数
    QVariantList result;
    QSqlQuery query;
    query.prepare("SELECT id,timewarn,sncontent FROM stickynote WHERE timewarn>0 AND snowner = ?");
    // 绑定实际值到第一个占位符
    query.addBindValue(userId);
    // 执行查询
    if (!query.exec()) {
        qDebug() << "Error executing query:" << query.lastError();
    }
    while (query.next()) {
        QVariantMap row;
        for (int i = 0; i < query.record().count(); ++i) {
            row[query.record().fieldName(i)] = query.value(i);
        }
        result.append(row);
    }
    return result;
}

QString LocalDataBase::readImmortalClass(int immortalClass){//读取用户便签数
    QSqlQuery query;
    query.prepare("SELECT classname FROM immortalclass WHERE id = ?");
    // 绑定实际值到第一个占位符
    query.addBindValue(immortalClass);
    // 执行查询
    if (!query.exec()) {
        qDebug() << "Error executing query:" << query.lastError();
    }
    if(query.next()) return query.value(0).toString();
    else return "";
}

int LocalDataBase::readCommandlistN(){//读取用户便签数
    QSqlQuery query;
    query.prepare("SELECT COUNT(*) FROM commandlist");
    // 执行查询
    if (!query.exec()) {
        qDebug() << "Error executing query:" << query.lastError();
    }
    if(query.next()) return query.value(0).toInt();
    else return -1;
}
QVariantList LocalDataBase::readCommand(int commandId){//读取用户便签数
    QVariantList result;
    QSqlQuery query;
    query.prepare("SELECT * FROM commandlist WHERE id = ?");
    // 绑定实际值到第一个占位符
    query.addBindValue(commandId);
    // 执行查询
    if (!query.exec()) {
        qDebug() << "Error executing query:" << query.lastError();
    }
    while (query.next()) {
        QVariantMap row;
        for (int i = 0; i < query.record().count(); ++i) {
            row[query.record().fieldName(i)] = query.value(i);
        }
        result.append(row);
    }
    return result;
}

QVariantList LocalDataBase::readCChildrens(int commandId){//读取用户便签数
    QVariantList result;
    QSqlQuery query;
    query.prepare("SELECT * FROM childrens WHERE me = ?");
    // 绑定实际值到第一个占位符
    query.addBindValue(commandId);
    // 执行查询
    if (!query.exec()) {
        qDebug() << "Error executing query:" << query.lastError();
    }
    while (query.next()) {
        QVariantMap row;
        for (int i = 0; i < query.record().count(); ++i) {
            row[query.record().fieldName(i)] = query.value(i);
        }
        result.append(row);
    }
    return result;
}

int LocalDataBase::readCPrerequisite(int commandId){//读取用户便签数
    QSqlQuery query;
    query.prepare("SELECT parent FROM prerequisite WHERE me = ?");
    // 绑定实际值到第一个占位符
    query.addBindValue(commandId);
    // 执行查询
    if (!query.exec()) {
        qDebug() << "Error executing query:" << query.lastError();
    }
    if(query.next()) return query.value(0).toInt();
    else return -1;
}

